package com.application.sistercityapplication.main;

import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import com.application.sistercityapplication.tabbitmap.TabBitmap;
import com.roscopeco.ormdroid.ORMDroidApplication;

@SuppressWarnings("deprecation")
public class MainSisterCityActivity extends TabActivity {
	private static final String TAG_1 = "HomeTab";
	private static final String TAG_2 = "DocumentTab";
	private static final String TAG_3 = "SettingTab";
	TabHost mTabHost;
	Intent intent;
	
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		ORMDroidApplication.initialize(MainSisterCityActivity.this);
//		if (android.os.Build.VERSION.SDK_INT > 9) {
//		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//		    StrictMode.setThreadPolicy(policy);
//		}
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main_sister_city);
		setTabs(savedInstanceState);		
		getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {

	        @Override
	        public void onClick(View v) {
	        //do whatever you need	        
	        	Intent i = new Intent(MainSisterCityActivity.this,MainSisterCityActivity.class);
	        	startActivity(i);
	        	overridePendingTransition(0, 0);
	        	finish();
	        	mTabHost.setCurrentTab(0);
	        }
		});
	}

	public void setTabs(Bundle savedInstanceState) {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		
		LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
		mLocalActivityManager.dispatchCreate(savedInstanceState);
		mTabHost.setup(mLocalActivityManager);
		
		intent = new Intent().setClass(MainSisterCityActivity.this, MainHomePageViewActivity.class);
		addTab(getResources().getString(R.string.MainHomePageText), TAG_1, createTabDrawable(R.drawable.home_footer),intent);
		
		intent = new Intent().setClass(MainSisterCityActivity.this, MainNewsDockViewActivity.class);
		addTab(getResources().getString(R.string.NewsDockText), TAG_2, createTabDrawable(R.drawable.doc_footer),intent);
		
		intent = new Intent().setClass(MainSisterCityActivity.this, MainSettingPageViewActivity.class);
		addTab(getResources().getString(R.string.SettingText), TAG_3, createTabDrawable(R.drawable.setting_footer),intent);
//		addTab("Settings", TAG_4, createTabDrawable(R.drawable.settings), R.id.text_view_settings);
		
		
	}
	
	private Drawable createTabDrawable(int resId) {
		Resources res = getResources();
		StateListDrawable states = new StateListDrawable();

		final Options options = new Options();
		options.inPreferredConfig = Config.ARGB_8888;
		
		Bitmap icon = BitmapFactory.decodeResource(res, resId, options);
		
		Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
		Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);
		
		icon.recycle();
		
		states.addState(new int[] { android.R.attr.state_selected }, new BitmapDrawable(res, selected));
		states.addState(new int[] { android.R.attr.state_enabled }, new BitmapDrawable(res, unselected));
		
		return states;
	}
	
	private View createTabIndicator(String label, Drawable drawable) {
		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);

		TextView txtTitle = (TextView) tabIndicator.findViewById(R.id.text_view_tab_title);
		txtTitle.setText(label);
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle.getLayoutParams();
		txtTitle.setLayoutParams(params);

		ImageView imgIcon = (ImageView) tabIndicator.findViewById(R.id.image_view_tab_icon);
		imgIcon.setImageDrawable(drawable);
		
		return tabIndicator;
	}
	
	private void addTab(String label, String tag, Drawable drawable,Intent intent) {
		TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
		spec.setIndicator(createTabIndicator(label, drawable));		
		spec.setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		mTabHost.addTab(spec);
	}

}
